﻿#include <windows.h>
#ifdef SMALL_CODE
	#include "twofish_small.h"
#else
	#include "twofish.h"
#endif

static const struct { /* see http://www.schneier.com/twofish.html */
	const unsigned char key[32];
	const unsigned char plaintext[16];
	const unsigned char ciphertext[16];
} twofish_vectors[] = {
	{ 
		{ 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10, 
		  0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xaa,0xbb,0xcc,0xdd,0xee,0xff },
		{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
		{ 0x37,0x52,0x7b,0xe0,0x05,0x23,0x34,0xb8,0x9f,0x0c,0xfc,0xca,0xe8,0x7c,0xfa,0x20 }
	},
	{
		{ 0xD4,0x3B,0xB7,0x55,0x6E,0xA3,0x2E,0x46,0xF2,0xA2,0x82,0xB7,0xD4,0x5B,0x4E,0x0D,
		  0x57,0xFF,0x73,0x9D,0x4D,0xC9,0x2C,0x1B,0xD7,0xFC,0x01,0x70,0x0C,0xC8,0x21,0x6F }, 
		{ 0x90,0xAF,0xE9,0x1B,0xB2,0x88,0x54,0x4F,0x2C,0x32,0xDC,0x23,0x9B,0x26,0x35,0xE6 }, 
		{ 0x6C,0xB4,0x56,0x1C,0x40,0xBF,0x0A,0x97,0x05,0x93,0x1C,0xB6,0xD4,0x08,0xE7,0xFA }
	}
};

int test_twofish256()
{
	twofish256_key skey;
	unsigned char  tmp[16];	
	int            i;
	
	for (i = 0; i < _countof(twofish_vectors); i++) 
	{
		twofish256_set_key(twofish_vectors[i].key, &skey);
		
		twofish256_encrypt(twofish_vectors[i].plaintext, tmp, &skey);
		if (memcmp(twofish_vectors[i].ciphertext, tmp, sizeof(tmp)) != 0) return 0;

		twofish256_decrypt(twofish_vectors[i].ciphertext, tmp, &skey);
		if (memcmp(twofish_vectors[i].plaintext, tmp, sizeof(tmp)) != 0) return 0;
	}	
	return 1;
}